﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using Telerik.Web.UI;
using Joseph.CareerPathApplication;

public partial class sandbox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string CompanyName = CompanyNameTextBox.Text;
        string root = Server.MapPath(@"~\Applications\");
        string path = root + CompanyName;
        DirectoryInfo di = new DirectoryInfo(root);
        DirectoryInfo di2 = new DirectoryInfo(path);
        //If the ~/Applications directory or the ~/Applications/ABCCompany does not exist
        // create it
        if (!di.Exists)
        {
            di.Create();
        }
        if (!di2.Exists)
        {
            di2.Create();
        }
        try
        {
            //If there is a file in both upload controls
            if (AdFileUpload.UploadedFiles.Count > 0 & upFile.UploadedFiles.Count > 0)
            {
                //Go through each uploaded file in the Application RadUpload 
                foreach (UploadedFile validFile in upFile.UploadedFiles)
                {
                    //Save the file in the applications folder
                    validFile.SaveAs(Path.Combine(path, validFile.GetName()), true);

                    //Go through each uploaded file in the job ad RadUpload
                    foreach (UploadedFile validFile2 in AdFileUpload.UploadedFiles)
                    {
                        //Save the the file in the applications folder
                        validFile2.SaveAs(Path.Combine(path, validFile2.GetName()), true);


                        //Insert the record into the database
                        #region
                        CareerPathDataContext db = new CareerPathDataContext();
                        cp_Application jA = new cp_Application
                        {
                            Date = Convert.ToDateTime(DatePicker.DbSelectedDate),
                            PositionName = PositionNameTextBox.Text,
                            CompanyName = CompanyNameTextBox.Text,
                            CompanyEMail = CompanyEMailTextBox.Text,
                            City = CompanyCityTextBox.Text,
                            State = "MA",
                            SourceID = int.Parse(RadComboBox1.SelectedValue),
                            //Using the for each loop, get the name of each file in
                            //the Application RadUpload, which is only 1
                            FilePath = validFile.GetName(),
                            //Using the for each loop, get the name of each file in
                            //the Job Ad RadUpload, which is only 1
                            AdPath = validFile2.GetName()
                        };
                        db.cp_Applications.InsertOnSubmit(jA);
                        db.SubmitChanges();
                    }
                }
                        #endregion

                //Send the user to a success page
                Response.Redirect("~/SuccessPage.aspx");
            }
            else
            {
                ErrorLabel.Text =
                ErrorLabel.Text = "Upload failed. You must upload both a valid job ad and a valid job application.";
            }
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = ex.ToString();
        }
        //If either of the uploads have any invalid files, show an error message
        try
        {
            if (upFile.InvalidFiles.Count > 0)
            {
                upFileError.Text = "Only .doc, .docx, rtf, and .txt files are allowed for the job application.";
            }
            if (AdFileUpload.InvalidFiles.Count > 0)
            {
                adFileError.Text = "Only .doc, .docx, rtf and .txt files are allowed for the job ad.";
            }
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = ex.ToString();
        }
    }
}
